package ae.pcfc.cldos.online.net.service.provider;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.soap.SoapMessage;

import ae.pcfc.cldos.online.net.ConstantsNET;
import ae.pcfc.cldos.online.net.service.BuisnessValidationsService;
import ae.pcfc.cldos.online.net.service.types.CheckCompanyVacant;
import ae.pcfc.cldos.online.net.service.types.CheckCompanyVacantResponse;
import ae.pcfc.cldos.online.net.service.types.IsEmployeeActive;
import ae.pcfc.cldos.online.net.service.types.IsEmployeeActiveResponse;
import ae.pcfc.cldos.online.net.service.types.IsEmployeeBanned;
import ae.pcfc.cldos.online.net.service.types.IsEmployeeBannedResponse;
import ae.pcfc.cldos.online.net.service.types.IsLicenseActive;
import ae.pcfc.cldos.online.net.service.types.IsLicenseActiveResponse;
import ae.pcfc.cldos.online.net.service.types.IsVisitorsExistByPassportName;
import ae.pcfc.cldos.online.net.service.types.IsVisitorsExistByPassportNameResponse;
import ae.pcfc.cldos.online.net.service.types.ValidateEmployeeTermination;
import ae.pcfc.cldos.online.net.service.types.ValidateEmployeeTerminationResponse;
import ae.pcfc.cldos.online.net.service.types.ValidateNocAndLetters;
import ae.pcfc.cldos.online.net.service.types.ValidateNocAndLettersResponse;
import ae.pcfc.cldos.online.net.service.types.ValidateOnlineEmpVisaData;
import ae.pcfc.cldos.online.net.service.types.ValidateOnlineEmpVisaDataResponse;
import ae.pcfc.cldos.online.net.service.types.ValidateOnlineEmploymentVisaRenewal;
import ae.pcfc.cldos.online.net.service.types.ValidateOnlineEmploymentVisaRenewalResponse;
import ae.pcfc.cldos.online.net.service.types.ValidateOnlineVisitVisaNew;
import ae.pcfc.cldos.online.net.service.types.ValidateOnlineVisitVisaNewResponse;
import ae.pcfc.cldos.online.ws.base.BaseWebService;

/**
 * 
 * @author arosa
 *
 */
public class BuisnessValidationsServiceImpl extends BaseWebService implements BuisnessValidationsService {
	
	@Value("${net.url}")
	String classURIValue = ConstantsNET.NET_WS_ENDPOINT;
	
	/**
	 * @param companyLicenseNo
	 * @return
	 * @throws Exception
	 */
	public boolean validateOnlineNOCLettersData(String companyLicenseNo) throws Exception{
		ValidateNocAndLetters validateNocAndLetters = new ValidateNocAndLetters();
		validateNocAndLetters.setCompanyLicenseNo(companyLicenseNo);
		ValidateNocAndLettersResponse response = (ValidateNocAndLettersResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, validateNocAndLetters,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.VALIDATE_NOC_LETTERS_DATA);
							}
						});
		return response.isValidateNocAndLettersResult();
	}
	/**
	 * @param companyLicenseNo
	 * @return
	 * @throws Exception
	 */
	public boolean validateOnlineEmpVisaTerminationData(String companyLicenseNo) throws Exception{
		ValidateEmployeeTermination validateEmployeeTermination = new ValidateEmployeeTermination();
		validateEmployeeTermination.setCompanyLicenseNo(companyLicenseNo);
		ValidateEmployeeTerminationResponse response = (ValidateEmployeeTerminationResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, validateEmployeeTermination,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.VALIDATE_EMP_VISA_TERMINATION_DATA);
							}
						});
		return response.isValidateEmployeeTerminationResult();
	}

	/**
	 * @param companyLicenseNo
	 * @param empId
	 * @return
	 * @throws Exception
	 */
	public boolean validateOnlineEmpVisaRenewalData(String companyLicenseNo, String empId) throws Exception{
		ValidateOnlineEmploymentVisaRenewal validateOnlineEmploymentVisaRenewal = new ValidateOnlineEmploymentVisaRenewal();
		validateOnlineEmploymentVisaRenewal.setCompanyLicenseNo(companyLicenseNo);
		validateOnlineEmploymentVisaRenewal.setEmployeeID(empId);
		
		ValidateOnlineEmploymentVisaRenewalResponse response = (ValidateOnlineEmploymentVisaRenewalResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, validateOnlineEmploymentVisaRenewal,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.VALIDATE_EMP_VISA_RENEWAL_DATA);
							}
						});
		return response.isValidateOnlineEmploymentVisaRenewalResult();
	}
	/**
	 * @param companyLicenseNo
	 * @param passportNameEn
	 * @param passportNo
	 * @return
	 * @throws Exception
	 */
	public boolean validateOnlineEmpVisaData(String companyLicenseNo, String passportNameEn, String passportNo) throws Exception{
		ValidateOnlineEmpVisaData validateEmploymentVisaData = new ValidateOnlineEmpVisaData();
		validateEmploymentVisaData.setCompanyLicenseNo(companyLicenseNo);
		validateEmploymentVisaData.setPassportNameEn(passportNameEn);
		validateEmploymentVisaData.setPassportNo(passportNo);
		ValidateOnlineEmpVisaDataResponse response = (ValidateOnlineEmpVisaDataResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, validateEmploymentVisaData,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.VALIDATE_EMPVISA_DATA);
							}
						});
		return response.isValidateOnlineEmpVisaDataResult();
	}

	/**
	 * @param companyLicenseNo
	 * @return
	 * @throws Exception
	 */
	public boolean validateOnlineVisitVisaData(String companyLicenseNo) throws Exception{
		ValidateOnlineVisitVisaNew validateOnlineVisitVisaNew = new ValidateOnlineVisitVisaNew();
		validateOnlineVisitVisaNew.setCompanyLicenseNo(companyLicenseNo);
		ValidateOnlineVisitVisaNewResponse response = (ValidateOnlineVisitVisaNewResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, validateOnlineVisitVisaNew,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.VALIDATE_VISITVISA_DATA);
							}
						});
		return response.isValidateOnlineVisitVisaNewResult();
	}
	public String isLicenceValid(String companyLicenseNo){
		IsLicenseActive isLicenseActive = new IsLicenseActive();
		isLicenseActive.setLicenseNum(companyLicenseNo);
		
		IsLicenseActiveResponse response = (IsLicenseActiveResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, isLicenseActive,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.LICENSE_CHECK_ENDPOINT);
							}
						});
		return response.getIsLicenseActiveResult();
	}
	
	public boolean isLeaseValid(String companyLicNo){
		/*IsLicenseLeaseValid isLicenseLeaseValid = new IsLicenseLeaseValid();
		isLicenseLeaseValid.setLicenseNum(companyLicNo);
		
		IsLicenseLeaseValidResponse response = (IsLicenseLeaseValidResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, isLicenseLeaseValid,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.LEASE_CHECK_ENDPOINT);
							}
						});
		return response.isIsLicenseLeaseValidResult();*/
		return true;
	}
	
	public boolean isEmployeeExist(String passportName, String passportNo){
		IsEmployeeActive isEmployeeActive = new IsEmployeeActive();
		isEmployeeActive.setPassportName(passportName);
		isEmployeeActive.setPassportID(passportNo);
		
		IsEmployeeActiveResponse response = (IsEmployeeActiveResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, isEmployeeActive,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.EMPLOYEE_EXISTANCE_CHECK_ENDPOINT);
							}
						});
		
		return response.isIsEmployeeActiveResult();
	}
	
	public boolean isEmployeeBaned(String passportName, String passportNo){
		IsEmployeeBanned isEmployeeBanned = new IsEmployeeBanned();
		isEmployeeBanned.setPassportNameEn(passportName);
		isEmployeeBanned.setPassportNo(passportNo);
		
		IsEmployeeBannedResponse response = (IsEmployeeBannedResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, isEmployeeBanned,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.EMPLOYEE_BAN_CHECK_ENDPOINT);
							}
						});
		
		return response.isIsEmployeeBannedResult();
	}
	
	public Integer getAviableEmployeesCount(String coLicNo){
		CheckCompanyVacant checkCompanyVacant = new CheckCompanyVacant();
		checkCompanyVacant.setLicenseNum(coLicNo);
		
		CheckCompanyVacantResponse response = (CheckCompanyVacantResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, checkCompanyVacant,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.COMPANY_VACANT_CHECK_ENDPOINT);
							}
						});
		
		return response.getCheckCompanyVacantResult();
	}
	
	public boolean isVisitorExist(String passportName, String passportNo){
		IsVisitorsExistByPassportName isVisitorsExistByPassportName = new IsVisitorsExistByPassportName();
		isVisitorsExistByPassportName.setPassportNameEn(passportName);
		isVisitorsExistByPassportName.setPassportNo(passportNo);
		
		IsVisitorsExistByPassportNameResponse response = (IsVisitorsExistByPassportNameResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, isVisitorsExistByPassportName,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.VISITOR_EXISTANCE_CHECK_ENDPOINT);
							}
						});
		
		return response.isIsVisitorsExistByPassportNameResult();
	}
	
}
